function Game(){
    // 游戏矩阵
    var gameData = [];
    for(var i=0; i<20; i++){
        gameData.push([])
        for(var j=0; j<10; j++){
            gameData[i].push(0)
        }
    }
    // 游戏divs元素
    var nextDivs = [];
    var gameDivs = [];
    var initDivs = function(data,domBox,divs){
        for(var i=0; i<data.length; i++){
            var divArr = [];
            for(var j=0; j<data[0].length; j++){
                var div = document.createElement('div');
                div.className = 'none';
                div.style.left = (j*20) + "px";
                div.style.top = (i*20) + "px";
                divArr.push(div);
                domBox.appendChild(div);
            }
            divs.push(divArr);
        }
    }
    // 更新游戏元素
    var updateDivNode = function(data,divs){
        for(var i=0; i<data.length; i++){
            for(var j=0; j<data[0].length; j++){
                if(data[i][j]===0){
                    divs[i][j].className = 'none';
                }else if(data[i][j]===1){
                    divs[i][j].className = 'done';
                }else if(data[i][j]===2){
                    divs[i][j].className = 'next';
                }
            }
        }
    }    

    // 初始化
    var gameDiv;
    var nextDiv;
    // 游戏区正在活动的方块
    var currentSquare;
    // 将要活动的方块
    var nextSquare;

    var initGame = function(doms){
        gameBox = doms.gameBox;
        nextBox = doms.nextBox;
        currentSquare = new Square();
        nextSquare = new Square();
        // 初始化游戏区方块
        initDivs(gameData,gameBox,gameDivs);
        // 将要活动方块展示
        initDivs(nextSquare.data,nextBox,nextDivs);
        // 将要活动方块放置——》游戏区(有起始点的复制)
        currentSquare.origin.x = 10;
        currentSquare.origin.y = 4;
        setUpdateData();
        updateDivNode(gameData,gameDivs);
        updateDivNode(nextSquare.data,nextDivs);
    }

    var check = function(pos,x,y){
        if(pos.x + x < 0){
            return false;
        }else if(pos.x + x >= gameData.length){
            return false;
        }else if(pos.y +y < 0){
            return false;
        }else if(pos.y + y >= gameData[0].length){
            return false;
        }else if(gameData[pos.x + x][pos.y + y] ===1){
            return false;
        }else {
            return true;
        }
    }

    var isValLegal = function(pos,data){
        for(var i=0; i<data.length; i++){
            for(var j=0; j<data[0].length; j++){
                if(data[i][j] != 0){
                    if(!check(pos,i,j)){
                        return false;
                    }
                }
            }
        }
        return true;
    }

    var setUpdateData = function(){
        for(var i=0; i<currentSquare.data.length; i++){
            for(var j=0; j<currentSquare.data[0].length; j++){
                if(check(currentSquare.origin,i,j)){
                    gameData[currentSquare.origin.x+i][currentSquare.origin.y+j] = currentSquare.data[i][j];
                }
            }
        }
    }

    var clearData = function(){
        for(var i=0; i<currentSquare.data.length; i++){
            for(var j=0; j<currentSquare.data[0].length; j++){
                if(check(currentSquare.origin,i,j)){
                    gameData[currentSquare.origin.x+i][currentSquare.origin.y+j] = 0;
                }
            }
        }
    }

    var directionHandel = function(direction){
        if(direction === 'up'){

        }else if(direction === 'left'){
            if(currentSquare.canLeft(isValLegal)){
                clearData();
                currentSquare.left();
                setUpdateData();
                updateDivNode(gameData,gameDivs);
            }
        }else if(direction ==='right'){
            if(currentSquare.canRight(isValLegal)){
                clearData();
                currentSquare.right();
                setUpdateData();
                updateDivNode(gameData,gameDivs);
            }
        }else if(direction === 'down'){
            if(currentSquare.canDown(isValLegal)){
                clearData();
                currentSquare.down();
                setUpdateData();
                updateDivNode(gameData,gameDivs);
            }
        }else if(direction ==='space'){

        }
    }

    // 导出initGame，供外部实例调用
    this.initGame = initGame;
    this.directionHandel = directionHandel;
}

